﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

public static class ExtendDataGridView
{
    public static DataTable ExportGridToDataTable(this System.Windows.Forms.DataGridView dgv)
    {
        if (dgv != null)
        {
            DataTable tbl = new DataTable();
            List<string> comboboxList = new List<string>();
            foreach (DataGridViewColumn dc in dgv.Columns)
            {
                if (dc.Visible)
                {
                    int count = 0;
                    string col_name = dc.HeaderText;
                    while (tbl.Columns.Contains(col_name))
                    {
                        count++;
                        col_name = dc.HeaderText + "(" + count.ToString() + ")";
                    }
                    if (dc.ValueType != null)
                        tbl.Columns.Add(new DataColumn(col_name, dc.ValueType));
                }
            }
            DateTime t1, t2;
            t1 = DateTime.Now;
            foreach (DataGridViewRow dr in dgv.Rows)
            {

                DataRow row = tbl.NewRow();
                foreach (DataGridViewCell dcc in dr.Cells)
                {
                    string column_name = dgv.Columns[dcc.ColumnIndex].HeaderText;
                    if (!tbl.Columns.Contains(column_name)) continue;
                    DataGridViewComboBoxCell combobox = dcc as DataGridViewComboBoxCell;
                    if (combobox != null)
                    {
                        var data = combobox.DataSource as BindingSource;
                        var table = (data.DataSource as DataSet).Tables[data.DataMember] as DataTable;
                        if (table != null)
                        {
                            var rows = table.Select(combobox.ValueMember + "='" + dcc.Value + "'");
                            if (rows.Any())
                                row[column_name] = rows[0][combobox.DisplayMember];
                        }
                    }
                    else
                    {
                        string string_value = dcc.FormattedValue.ToString();
                        if (dcc.ValueType == typeof(decimal))
                        {
                            decimal value = 0;
                            if (!decimal.TryParse(string_value, out value))
                                row[column_name] = DBNull.Value;
                            else row[column_name] = value;
                        }
                        else if (dcc.ValueType == typeof(DateTime))
                        {
                            DateTime value = new DateTime();
                            if (!DateTime.TryParse(string_value, out value))
                                row[column_name] = DBNull.Value;
                            else row[column_name] = value;
                        }
                        else if (dcc.ValueType == typeof(int))
                        {
                            int value = 0;
                            if (!int.TryParse(string_value, out value))
                                row[column_name] = DBNull.Value;
                            else row[column_name] = value;
                        }
                        else if (dcc.ValueType == typeof(bool))
                        {
                            bool value = true;
                            if (!bool.TryParse(string_value, out value))
                                row[column_name] = DBNull.Value;
                            else row[column_name] = value;
                        }
                        else//其餘都當作是字串處理
                        {
                            row[column_name] = string_value;
                        }
                    }
                }
                tbl.Rows.Add(row);
            }
            t2 = DateTime.Now;
            var ts = t2 - t1;
            return tbl;
        }

        return null;
    }
}

